home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / answers / motif-faq / part3 < prev    next >
Encoding:
Text File  |  1993-06-30  |  44.0 KB  |  1,173 lines

  1. Newsgroups: comp.windows.x.motif,news.answers,comp.answers
  2. Path: senator-bedfellow.mit.edu!enterpoop.mit.edu!gatech!usenet.ufl.edu!usenet.cis.ufl.edu!caen!batcomputer!munnari.oz.au!newshost.anu.edu.au!csc.canberra.edu.au!news
  3. From: jan@ise.canberra.edu.au (Jan Newmarch)
  4. Subject: Motif FAQ (Part 3 of 5)
  5. Message-ID: <1993Jul1.011140.27974@csc.canberra.edu.au>
  6. Followup-To: comp.windows.x.motif
  7. Keywords: FAQ question answer
  8. Sender: news@csc.canberra.edu.au
  9. Reply-To: jan@ise.canberra.edu.au (Jan Newmarch)
  10. Organization: University of Canberra
  11. Date: Thu, 1 Jul 93 01:11:40 GMT
  12. Approved: news-answers-request@MIT.Edu
  13. Expires: +1 months
  14. Lines: 1156
  15. Xref: senator-bedfellow.mit.edu comp.windows.x.motif:18408 news.answers:9857 comp.answers:1162
  16.  
  17. Archive-name: motif-faq/part3
  18. Last-modified: Wed July 1 1993
  19. Version: 3.6
  20.  
  21.  
  22.  
  23.  
  24. -----------------------------------------------------------------------------
  25. Subject: 58) TOPIC: FORM WIDGET
  26.  
  27.  
  28. -----------------------------------------------------------------------------
  29. Subject: 59) Why don't labels in a Form resize when the label is changed?
  30. I've got some labels in a form. The labels don't resize whenever the label
  31. string resource is changed. As a result, the operator has to resize the window
  32. to see the new label contents. I am using Motif 1.1.
  33.  
  34. Answer: This problem may happen to any widget inside a Form widget. The
  35. problem was that the Form will resize itself when it gets geometry requests
  36. from its children. If its preferred size is not allowed, the Form will
  37. disallow all geometry requests from its children. The workaround is that you
  38. should set any ancestor of the Form to be resizable. For the shell which
  39. contains the Form you should set the shell resource XmNallowShellResize to be
  40. True (by default, it is set to FALSE).  There is currently an inconsistency on
  41. how resizing is being done, and it may get fixed in Motif 1.2.
  42.  
  43. From db@sunbim.be (Danny Backx)
  44.  
  45. Basically what you have to do is set the XmNresizePolicy on the Form to
  46. XmRESIZE_NONE.  The facts seem to be that XmRESIZE_NONE does NOT mean "do not
  47. allow resizes".  You may also have to set XmNresizable on the form to True.
  48.  
  49. -----------------------------------------------------------------------------
  50. Subject: 60) How can I center a widget in a form?
  51.  
  52. Answer: One of Motif's trickier questions.  The problems are that: Form gives
  53. no support for centering, only for edge attachments, and the widget must stay
  54. in the center if the form or the widget is resized.  Just looking at
  55. horizontal centering (vertical is similar) some solutions are:
  56.  
  57.  a.  Use the table widget instead of Form.
  58.  
  59.  b.  A hack free solution is from Dan Heller:
  60.  
  61.      /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  62.       * This program is freely distributable without licensing fees and
  63.       * is provided without guarantee or warranty expressed or implied.
  64.       * This program is -not- in the public domain.  This program is
  65.       * taken from the Motif Programming Manual, O'Reilly Volume 6.
  66.       */
  67.  
  68.      /* corners.c -- demonstrate widget layout management for a
  69.       * BulletinBoard widget.  There are four widgets each labeled
  70.       * top-left, top-right, bottom-left and bottom-right.  Their
  71.       * positions in the bulletin board correspond to their names.
  72.       * Only when the widget is resized does the geometry management
  73.       * kick in and position the children in their correct locations.
  74.       */
  75.      #include <Xm/BulletinB.h>
  76.      #include <Xm/PushBG.h>
  77.  
  78.      char *corners[] = {
  79.          "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right",
  80.      };
  81.  
  82.      static void resize();
  83.  
  84.      main(argc, argv)
  85.      int argc;
  86.      char *argv[];
  87.      {
  88.          Widget toplevel, bboard;
  89.          XtAppContext app;
  90.          XtActionsRec rec;
  91.          int i;
  92.  
  93.          /* Initialize toolkit and create toplevel shell */
  94.          toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  95.              &argc, argv, NULL, NULL);
  96.  
  97.          /* Create your standard BulletinBoard widget */
  98.          bboard = XtVaCreateManagedWidget("bboard",
  99.              xmBulletinBoardWidgetClass, toplevel, NULL);
  100.  
  101.          /* Set up a translation table that captures "Resize" events
  102.           * (also called ConfigureNotify or Configure events).  If the
  103.           * event is generated, call the function resize().
  104.           */
  105.          rec.string = "resize";
  106.          rec.proc = resize;
  107.          XtAppAddActions(app, &rec, 1);
  108.          XtOverrideTranslations(bboard,
  109.              XtParseTranslationTable("<Configure>: resize()"));
  110.  
  111.          /* Create children of the dialog -- a PushButton in each corner. */
  112.          for (i = 0; i < XtNumber(corners); i++)
  113.              XtVaCreateManagedWidget(corners[i],
  114.                  xmPushButtonGadgetClass, bboard, NULL);
  115.  
  116.          XtRealizeWidget(toplevel);
  117.          XtAppMainLoop(app);
  118.      }
  119.  
  120.      /* resize(), the routine that is automatically called by Xt upon the
  121.       * delivery of a Configure event.  This happens whenever the widget
  122.       * gets resized.
  123.       */
  124.      static void
  125.      resize(w, event, args, num_args)
  126.      CompositeWidget w;   /* The widget (BulletinBoard) that got resized */
  127.      XConfigureEvent *event;  /* The event struct associated with the event */
  128.      String args[]; /* unused */
  129.      int *num_args; /* unused */
  130.      {
  131.          WidgetList children;
  132.          int width = event->width;
  133.          int height = event->height;
  134.          Dimension w_width, w_height;
  135.          short margin_w, margin_h;
  136.  
  137.          /* get handle to BulletinBoard's children and marginal spacing */
  138.          XtVaGetValues(w,
  139.              XmNchildren, &children,
  140.              XmNmarginWidth, &margin_w,
  141.              XmNmarginHeight, &margin_h,
  142.              NULL);
  143.  
  144.          /* place the top left widget */
  145.          XtVaSetValues(children[0],
  146.              XmNx, margin_w,
  147.  
  148.              XmNy, margin_h,
  149.              NULL);
  150.  
  151.          /* top right */
  152.          XtVaGetValues(children[1], XmNwidth, &w_width, NULL);
  153.  
  154.          /* To Center a widget in the middle of the BulletinBoard (or Form),
  155.           * simply call:
  156.           *   XtVaSetValues(widget,
  157.                XmNx,    (width - w_width)/2,
  158.                XmNy,    (height - w_height)/2,
  159.                NULL);
  160.           * and return.
  161.           */
  162.          XtVaSetValues(children[1],
  163.              XmNx, width - margin_w - w_width,
  164.              XmNy, margin_h,
  165.              NULL);
  166.          /* bottom left */
  167.          XtVaGetValues(children[2], XmNheight, &w_height, NULL);
  168.          XtVaSetValues(children[2],
  169.  
  170.              XmNx, margin_w,
  171.              XmNy, height - margin_h - w_height,
  172.              NULL);
  173.          /* bottom right */
  174.          XtVaGetValues(children[3],
  175.              XmNheight, &w_height,
  176.              XmNwidth, &w_width,
  177.              NULL);
  178.          XtVaSetValues(children[3],
  179.              XmNx, width - margin_w - w_width,
  180.              XmNy, height - margin_h - w_height,
  181.              NULL);
  182.      }
  183.  
  184.  c.  No uil solution has been suggested, because of the widget size problem
  185.  
  186. -----------------------------------------------------------------------------
  187. Subject: 61) How do I line up two columns of widgets of different types?  I
  188. have a column of say label widgets, and a column of text widgets and I want to
  189. have them lined up horizontally. The problem is that they are of different
  190. heights. Just putting them in a form or rowcolumn doesn't line them up
  191. properly because the label and text widgets are of different height.
  192.  
  193. If you want the geometry to look like this
  194.  
  195.           -------------------------------------
  196.          |          -------------------------- |
  197.          |a label  |Some text                 ||
  198.          |          -------------------------- |
  199.                            ------------------- |
  200.          |a longer label  |Some more text     ||
  201.          |                 ------------------- |
  202.          |                    ---------------- |
  203.          |a very long label  |Even more text  ||
  204.          |                    ---------------- |
  205.           -------------------------------------
  206.  
  207. try
  208.  
  209. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  210.  * This program is freely distributable without licensing fees and
  211.  * is provided without guarantee or warranty expressed or implied.
  212.  * This program is -not- in the public domain.  This program is
  213.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  214.  */
  215.  
  216. /* text_form.c -- demonstrate how attachments work in Form widgets.
  217.  * by creating a text-entry form type application.
  218.  */
  219.  
  220. #include <Xm/PushB.h>
  221. #include <Xm/PushBG.h>
  222. #include <Xm/LabelG.h>
  223. #include <Xm/Text.h>
  224. #include <Xm/Form.h>
  225.  
  226. char *prompts[] = {
  227.     "Name:", "Phone:", "Address:",
  228.     "City:", "State:", "Zip:",
  229. };
  230.  
  231. main(argc, argv)
  232. int argc;
  233. char *argv[];
  234. {
  235.     Widget toplevel, mainform, subform, label, text;
  236.     XtAppContext app;
  237.     char buf[32];
  238.     int i;
  239.  
  240.     toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  241.         &argc, argv, NULL, NULL);
  242.  
  243.     mainform = XtVaCreateWidget("mainform",
  244.         xmFormWidgetClass, toplevel,
  245.         NULL);
  246.  
  247.     for (i = 0; i < XtNumber(prompts); i++) {
  248.         subform = XtVaCreateWidget("subform",
  249.             xmFormWidgetClass,   mainform,
  250.             /* first one should be attached for form */
  251.             XmNtopAttachment,    i? XmATTACH_WIDGET : XmATTACH_FORM,
  252.             /* others are attached to the previous subform */
  253.             XmNtopWidget,        subform,
  254.             XmNleftAttachment,   XmATTACH_FORM,
  255.             XmNrightAttachment,  XmATTACH_FORM,
  256.             NULL);
  257.         label = XtVaCreateManagedWidget(prompts[i],
  258.             xmLabelGadgetClass,  subform,
  259.             XmNtopAttachment,    XmATTACH_FORM,
  260.             XmNbottomAttachment, XmATTACH_FORM,
  261.             XmNleftAttachment,   XmATTACH_FORM,
  262.             XmNalignment,        XmALIGNMENT_BEGINNING,
  263.             NULL);
  264.         sprintf(buf, "text_%d", i);
  265.         text = XtVaCreateManagedWidget(buf,
  266.             xmTextWidgetClass,   subform,
  267.             XmNtopAttachment,    XmATTACH_FORM,
  268.             XmNbottomAttachment, XmATTACH_FORM,
  269.             XmNrightAttachment,  XmATTACH_FORM,
  270.             XmNleftAttachment,   XmATTACH_WIDGET,
  271.             XmNleftWidget,       label,
  272.             NULL);
  273.         XtManageChild(subform);
  274.     }
  275.     /* Now that all the forms are added, manage the main form */
  276.     XtManageChild(mainform);
  277.  
  278.     XtRealizeWidget(toplevel);
  279.     XtAppMainLoop(app);
  280. }
  281.  
  282. If you resize horizontally it stretches the text widgets.  If you resize
  283. vertically it leaves space under the bottom (if you don't resize, this is not
  284. problem).
  285.  
  286.           ----------------------------------------
  287.          |                    ------------------- |
  288.          |          a label  |Some text          ||
  289.          |                    ------------------- |
  290.                               ------------------- |
  291.          |   a longer label  |Some more text     ||
  292.          |                    ------------------- |
  293.          |                    ------------------- |
  294.          |a very long label  |Even more text     ||
  295.          |                    ------------------- |
  296.           ----------------------------------------
  297.  
  298. try this
  299.  
  300. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  301.  * This program is freely distributable without licensing fees and
  302.  * is provided without guarantee or warranty expressed or implied.
  303.  * This program is -not- in the public domain.  This program is
  304.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  305.  */
  306.  
  307. /* text_entry.c -- This demo shows how the RowColumn widget can be
  308.  * configured to build a text entry form.  It displays a table of
  309.  * right-justified Labels and Text widgets that extend to the right
  310.  * edge of the Form.
  311.  */
  312. #include <Xm/LabelG.h>
  313. #include <Xm/RowColumn.h>
  314. #include <Xm/Text.h>
  315.  
  316. char *text_labels[] = {
  317.     "Name:", "Phone:", "Address:", "City:", "State:", "Zip:",
  318. };
  319.  
  320. main(argc, argv)
  321. int argc;
  322. char *argv[];
  323. {
  324.     Widget toplevel, rowcol;
  325.     XtAppContext app;
  326.     char buf[8];
  327.     int i;
  328.  
  329.     toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  330.         &argc, argv, NULL, NULL);
  331.  
  332.     rowcol = XtVaCreateWidget("rowcolumn",
  333.         xmRowColumnWidgetClass, toplevel,
  334.         XmNpacking,        XmPACK_COLUMN,
  335.         XmNnumColumns,     XtNumber(text_labels),
  336.         XmNorientation,    XmHORIZONTAL,
  337.         XmNisAligned,      True,
  338.         XmNentryAlignment, XmALIGNMENT_END,
  339.         NULL);
  340.  
  341.     /* simply loop thru the strings creating a widget for each one */
  342.     for (i = 0; i < XtNumber(text_labels); i++) {
  343.         XtVaCreateManagedWidget(text_labels[i],
  344.             xmLabelGadgetClass, rowcol,
  345.             NULL);
  346.         sprintf(buf, "text_%d", i);
  347.         XtVaCreateManagedWidget(buf,
  348.             xmTextWidgetClass, rowcol,
  349.             NULL);
  350.     }
  351.  
  352.     XtManageChild(rowcol);
  353.     XtRealizeWidget(toplevel);
  354.     XtAppMainLoop(app);
  355. }
  356.  
  357. This makes all objects exactly the same size.  It does not resize in nice
  358. ways.
  359.  
  360. If you want the text widgets lined up on the left, and the labels to be the
  361. size of the longest string, resizing nicely both horizontally and vertically,
  362. as in
  363.  
  364.          -------------------------------------
  365.         |                    ---------------- |
  366.         |          a label  |Some text       ||
  367.         |                    ---------------- |
  368.                              ---------------- |
  369.         |   a longer label  |Some more text  ||
  370.         |                    ---------------- |
  371.         |                    ---------------- |
  372.         |a very long label  |Even more text  ||
  373.         |                    ---------------- |
  374.          -------------------------------------
  375.  
  376.  
  377.  
  378. Answer: Do this: to get the widgets lined up horizontally, use a form but
  379. place the widgets using XmATTACH_POSITION.  In the example, attach the top of
  380. the first label to the form, the bottomPosition to 33 (33% of the height).
  381. Attach the topPosition of the second label to 33 and the bottomPosition to 66.
  382. Attach the topPosition of the third label to 66 and the bottom of the label to
  383. the form.  Do the same with the text widgets.
  384.  
  385. To get the label widgets lined up vertically, use the right attachment of
  386. XmATTACH_OPPOSITE_WIDGET: starting from the one with the longest label, attach
  387. widgets on the right to each other. In the example, attach the 2nd label to
  388. the third, and the first to the second.  To get the text widgets lined up,
  389. just attach them on the left to the labels.  To get the text in the labels
  390. aligned correctly, use XmALIGNMENT_END for the XmNalignment resource.
  391.  
  392.         /* geometry for label 2
  393.         */
  394.         n = 0;
  395.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  396.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  397.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  398.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  399.         XtSetArg (args[n], XmNtopPosition, 66); n++;
  400.         XtSetValues (label[2], args, n);
  401.  
  402.         /* geometry for label 1
  403.         */
  404.         n = 0;
  405.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  406.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  407.         XtSetArg (args[n], XmNbottomPosition, 66); n++;
  408.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  409.         XtSetArg (args[n], XmNtopPosition, 33); n++;
  410.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  411.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  412.         XtSetArg (args[n], XmNrightWidget, label[2]); n++;
  413.         XtSetValues (label[1], args, n);
  414.  
  415.         /* geometry for label 0
  416.         */
  417.         n = 0;
  418.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  419.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  420.         XtSetArg (args[n], XmNbottomPosition, 33); n++;
  421.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  422.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  423.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  424.         XtSetArg (args[n], XmNrightWidget, label[1]); n++;
  425.         XtSetValues (label[0], args, n);
  426.  
  427.         /* geometry for text 0
  428.         */
  429.         n = 0;
  430.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  431.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  432.         XtSetArg (args[n], XmNbottomPosition, 33); n++;
  433.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  434.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  435.         XtSetArg (args[n], XmNleftWidget, label[0]); n++;
  436.         XtSetValues (text[0], args, n);
  437.  
  438.         /* geometry for text 1
  439.         */
  440.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  441.         XtSetArg (args[n], XmNtopPosition, 33); n++;
  442.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  443.         XtSetArg (args[n], XmNbottomPosition, 66); n++;
  444.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  445.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  446.         XtSetArg (args[n], XmNleftWidget, label[1]); n++;
  447.         XtSetValues (text[1], args, n);
  448.  
  449.         /* geometry for text 2
  450.         */
  451.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  452.         XtSetArg (args[n], XmNtopPosition, 66); n++;
  453.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  454.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  455.         XtSetArg (args[n], XmNleftWidget, label[2]); n++;
  456.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  457.         XtSetValues (text[2], args, n);
  458.  
  459.  
  460. -----------------------------------------------------------------------------
  461. Subject: 62) TOPIC: PUSHBUTTON WIDGET
  462.  
  463. -----------------------------------------------------------------------------
  464. Subject: 63) Why can't I use accelerators on buttons not in a menu?
  465.  
  466. Answer: It is apparently a difficult feature to implement, but OSF are
  467. considering this for the future. It is problematic trying to use the Xt
  468. accelerators since the Motif method interferes with this.  The workaround
  469. suggested so far is to duplicate your non-menu button by a button in a menu
  470. somewhere, which does have a menu-accelerator installed.  When the user
  471. invokes what they think is the accelerator for the button they can see Motif
  472. actually invokes the button on the menu that they can't see at the time.
  473.  
  474.  
  475. -----------------------------------------------------------------------------
  476. Subject: 64) TOPIC: LABEL WIDGET
  477.  
  478. -----------------------------------------------------------------------------
  479. Subject: 65) How can I align the text in a label (button, etc) widget?
  480.  
  481. Answer: The alignment for the label widget is controlled by the resource
  482. XmNalignment, and the default centers the text. Use this resource to change it
  483. to left or right alignment.  However, when the label (or any descendant) is in
  484. a row column, and XmNisAligned is True (the default), the row column aligns
  485. text using its resource XmNentryAlignment. If you want simultaneous control
  486. over all widgets use this, but otherwise turn XmNisAligned off and do it
  487. individually.
  488.  
  489.  
  490.  
  491. -----------------------------------------------------------------------------
  492. Subject: 66) Why doesn't label alignment work in a RowColumn?
  493.  
  494. Answer: RowColumn has a  resource XmNisAligned (default True) and and
  495. XmNentryAlignment (default XmALIGNMENT_BEGINNING).  These control alignment of
  496. the labelString in Labels and descendants. Set XmNisAligned to False to turn
  497. this off.
  498.  
  499. -----------------------------------------------------------------------------
  500. Subject: 67)  How can I set a multiline label?
  501. [Last modified: September 92]
  502.  
  503. Answer: In .Xdefaults
  504.  
  505.       *XmLabel*labelString:             Here\nis\nthe\nLabel
  506.  
  507. This method does not seem to work in some of the older Motif 1.0 versions.
  508.  
  509. In code,
  510.  
  511.       char buf[128];
  512.       XmString msg;
  513.       sprintf(buf, "Here\nis\nthe\nLabel");
  514.       msg = XmStringCreateLtoR(buf, XmSTRING_DEFAULT_CHARSET);
  515.       XtSetArg (args[n], XmNlabelString, msg);
  516.  
  517. Gives a four line label, using the escape sequence \n for a newline.  However,
  518. XmStringCreateLtoR() is obsoleted from version 1.1 on, and may disappear.
  519. This is because it it is only in the AES as "trial-use" and has been proposed
  520. for removal from the AES. Realistically, it will probably not be removed from
  521. any backward compatible versions of Motif, but the potential is there.  If it
  522. does disappear (or if you want to avoid using the non-AES compliant
  523. XmSTRING_DEFAULT_CHARSET), try this from Jean-Philippe Martin-Flatin
  524. <syj@ecmwf.co.uk>
  525.  
  526. #include <Xm/Xm.h>
  527. #include <string.h>
  528.  
  529. /*-----------------------------------------------------
  530.     Create a new XmString from a char*
  531.  
  532.     This function can deal with embedded 'newline' and
  533.     is equivalent to the obsolete XmStringCreateLtoR,
  534.     except it does not use non AES compliant charset
  535.     XmSTRING_DEFAULT_CHARSET
  536. ----------------------------------------------------*/
  537. XmString xec_NewString(char *s)
  538. {
  539.     XmString xms1;
  540.     XmString xms2;
  541.     XmString line;
  542.     XmString separator;
  543.     char     *p;
  544.     char     *t = XtNewString(s);   /* Make a copy for strtok not to */
  545.                                     /* damage the original string    */
  546.  
  547.  
  548.     separator = XmStringSeparatorCreate();
  549.     p         = strtok(t,"\n");
  550.     xms1      = XmStringCreateSimple(p);
  551.  
  552.     while (p = strtok(NULL,"\n"))
  553.     {
  554.         line = XmStringCreateSimple(p);
  555.         xms2 = XmStringConcat(xms1,separator);
  556.         XmStringFree(xms1);
  557.         xms1 = XmStringConcat(xms2,line);
  558.         XmStringFree(xms2);
  559.         XmStringFree(line);
  560.     }
  561.  
  562.     XmStringFree(separator);
  563.     XtFree(t);
  564.     return xms1;
  565. }
  566.  
  567.  
  568. Do not use XmStringCreateSimple() - it does not process the newline character
  569. in the way you want.
  570.  
  571. In UIL, you have to explicitly create a compound string with a separator.
  572. Here's what W. Scott Meeks suggests:
  573.  
  574. value nl : compound_string('', seperate=true);
  575.  
  576. object my_label : XmLabel
  577. {
  578.     arguments
  579.     {
  580.         XmNlabelString = 'Here' & nl & 'is' & nl & 'the' & nl & 'Label';
  581.     };
  582. };
  583.  
  584.  
  585. -----------------------------------------------------------------------------
  586. Subject: 68)  How can I have a vertical label?
  587.  
  588. Answer: Make a multiline label with one character per line, as in the last
  589. question. There is no way to make the text rotated by 90 degrees though.
  590.  
  591.  
  592. -----------------------------------------------------------------------------
  593. Subject: 69)  How can I have a Pixmap in a Label?
  594.  
  595. Answer: From Bob Hays (bobhays@spss.com)
  596.  
  597.     Pixmap px_disarm, px_disarm_insens;
  598.  
  599.     Widget Label1;
  600.     Pixel   foreground, background;
  601.     Arg     args[4];
  602.     Arg     arg[] = {
  603.                 { XmNforeground, &foreground },
  604.                 { XmNbackground, &background }
  605.     };
  606.  
  607.     Label1 = XmCreateLabel ( Shell1, "Label1",
  608.                                        (Arg *) NULL, (Cardinal) 0 );
  609.     XtGetValues ( Label1, arg, XtNumber ( arg ) );
  610.     px_disarm =
  611.       XCreatePixmapFromBitmapData(display,
  612.                                 DefaultRootWindow(display),
  613.                                 mtn_bits, mtn_width, mtn_height,
  614.                                 foreground,
  615.                                 background,
  616.                                 DefaultDepth(display,DefaultScreen(display)));
  617.     px_disarm_insens =
  618.       XCreatePixmapFromBitmapData(display,
  619.                                 DefaultRootWindow(display),
  620.                                 mtn_ins_bits, mtn_ins_width, mtn_ins_height,
  621.                                 foreground,
  622.                                 background,
  623.                                 DefaultDepth(display,DefaultScreen(display)));
  624.  
  625.     n = 0;
  626.     XtSetArg(args[n], XmNlabelType, XmPIXMAP);  n++;
  627.     XtSetArg(args[n], XmNlabelPixmap, px_disarm);  n++;
  628.     XtSetArg(args[n], XmNlabelInsensitivePixmap, px_disarm_insens ); n++;
  629.     XtSetValues ( Label1, args, n );
  630.     XtManageChild(Label1);
  631.  
  632. That will cause the foreground and background of your pixmap to be inherited
  633. from the one that would be used by OSF/Motif when the label is displayed.  The
  634. advantage is that this will utilize any resource values the user may have
  635. requested without looking explicitly into the resource database.  And, you
  636. will have a pixmap handy if the application insensitizes the label (without an
  637. XmNlabelInsensitivePixmap your label will go empty if made insensitive).
  638.  
  639. [Bob's original code was for a PushButton. Just change all Label to PushButton
  640. for them.]
  641.  
  642.  
  643. -----------------------------------------------------------------------------
  644. Subject: 70) TOPIC: DRAWING AREA WIDGET
  645.  
  646. -----------------------------------------------------------------------------
  647. Subject: 71) How can I send an expose event to a Drawing Area widget?  (or any
  648. other, come to that). I want to send an expose event so that it will redraw
  649. itself.
  650.  
  651. Answer: Use the Xlib call
  652.  
  653.         XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True)
  654.  
  655. This clears the widget's window and generates an expose event in doing so.
  656. The widgets expose action will then redraw it.  This uses a round trip
  657. request.  An alternative, without the round trip is
  658.  
  659. from orca!mesa!rthomson@uunet.uu.net  (Rich Thomson):
  660.  
  661.     Widget da;
  662.     XmDrawingAreaCallbackStruct da_struct;
  663.  
  664.     da_struct.reason = XmCR_EXPOSE;
  665.     da_struct.event = (XEvent *) NULL;
  666.     da_struct.window = XtWindow(da);
  667.  
  668.     XtCallCallbacks(da, XmNexposeCallback, (XtPointer) da_struct);
  669.  
  670.  
  671. -----------------------------------------------------------------------------
  672. Subject: 72) How can I know when a DrawingArea has been resized?  It generates
  673. an expose event whn it is enlarged, but not when it is shrunk.
  674.  
  675. Answer: Use the resize callback.
  676.  
  677. -----------------------------------------------------------------------------
  678. Subject: 73) TOPIC: MENUS
  679.  
  680. -----------------------------------------------------------------------------
  681. Subject: 74) What can I put inside a menu bar?
  682.  
  683. Answer: You can only put cascade buttons in menu bars. No pushbuttons, toggle
  684. buttons or gadgets are allowed. When you create a pulldown menu with parent a
  685. menu bar, its real parent is a shell widget.
  686.  
  687. -----------------------------------------------------------------------------
  688. Subject: 75) Can I have a cascade button without a submenu in a pulldown menu?
  689.  
  690. Answer: Yes you can. A cascade button has an activate callback which is called
  691. when you click on it and it doesn't have a submenu. It can have a mnemonic,
  692. but keyboard traversal using the arrow keys in the menu will skip over it.
  693.  
  694. -----------------------------------------------------------------------------
  695. Subject: 76) Should I have a cascade button without a submenu in a pulldown
  696. menu?
  697.  
  698. Answer: No. This is forbidden by the style guide. Technically you can do it
  699. (see previous question) but if you do it will not be Motif style compliant.
  700. This is unlikely to change - if a "button" is important enough to be in a
  701. pulldown menu bar with no pulldown, it should be a button elsewhere.  (Mind
  702. you, you won't be able to put accelerators on it elsewhere though.)
  703.  
  704. -----------------------------------------------------------------------------
  705. Subject: 77)  What is the best way to create popup menus?
  706. [Last modified: August 92]
  707.  
  708. Susan Murdock Thompson (from OSF): In general, create a popupMenu as the child
  709. from which you will be posting it from (ie: if you have a bulletinBoard with a
  710. PushButton in it and want MB2 on the pushButton to post the popupMenu, create
  711. the popupMenu as a child of the pushButton).  [This parent-child relationship
  712. seems to make a big difference in the behavior of the popups.]  Add an event
  713. handler to handle buttonPress events.  You'll need to check for the correct
  714. button (what you've specified menuPost to be) before posting the menu.
  715.  
  716. To create a popup that can be accessible from within an entire client window,
  717. create it as the child of the top-most widget (but not the shell) and add
  718. event handlers for the top-most widget and children widgets.
  719.  
  720. ie:
  721.  
  722. {
  723.   ....
  724.  
  725.   XtManageChild(rc=XmCreateRowColumn(Shell1, "rc", NULL, 0));
  726.   XtManageChild(label = XmCreateLabel(rc, "label", NULL, 0));
  727.   XtManageChild(text = XmCreateText(rc, "text", NULL, 0));
  728.   XtManageChild(pushbutton = XmCreatePushButton(rc, "pushbutton", NULL, 0));
  729.  
  730.   n = 0;
  731.   XtSetArg(args[n], XmNmenuPost, "<Btn3Down>"); n++;
  732.   popup = XmCreatePopupMenu(rc, "popup", args, n);
  733.  
  734.   XtAddEventHandler(rc, ButtonPressMask, False, PostMenu3, popup);
  735.   XtAddEventHandler(text, ButtonPressMask, False, PostMenu3, popup);
  736.   XtAddEventHandler(label, ButtonPressMask, False, PostMenu3, popup);
  737.   XtAddEventHandler(pushbutton, ButtonPressMask, False, PostMenu3, popup);
  738.  
  739.   XtManageChild(m1 = XmCreatePushButton(popup, "m1", NULL, 0));
  740.   XtManageChild(m2 = XmCreatePushButton(popup, "m2", NULL, 0));
  741.   XtManageChild(m3 = XmCreatePushButton(popup, "m3", NULL, 0));
  742.  
  743.   XtAddCallback(m1, XmNactivateCallback, SayCB, "button M1");
  744.   XtAddCallback(m2, XmNactivateCallback, SayCB, "button M2");
  745.   XtAddCallback(m3, XmNactivateCallback, SayCB, "button M3");
  746.   ...
  747. }
  748.  
  749. /* where PostMenu3 is ... */
  750.  
  751. PostMenu3 (w, popup, event)
  752. Widget w;
  753. Widget popup;
  754. XButtonEvent * event;
  755. {
  756.   printf("menuPost = 3, button %d0, event->button);
  757.  
  758.   if (event->button != Button3)
  759.     return;
  760.   XmMenuPosition(popup, event);
  761.   XtManageChild(popup);
  762. }
  763.  
  764.  
  765.  
  766. -----------------------------------------------------------------------------
  767. Subject: 78)  How do popup menus work?
  768. [Last modified: August 92]
  769.  
  770. Answer:
  771.  
  772. When a popup menu is created as the child of a widget the menu system installs
  773. a translation on the parent of the popup and descendants with an action which:
  774. (1) when 3-rd button (the default for the menuPost resource) is pressed the
  775. cursor changes and the mouse is grabbed for 5 seconds; (2) disables event
  776. handlers on the descendants and the handlers are never called; (3) an event
  777. handler installed on the parent works fine.
  778.  
  779. It is done so that the correct event handler will (in fact) be called.  There
  780. is a grab with owner_events true.  The grab is released by a timer,  but
  781. normally the posted menu shell puts up it's own grab.
  782.  
  783. If you only have widgets then you can use the subwindow field in the event to
  784. identify the original widget.  If you have gadgets or other data that you want
  785. to change the menu for (or use a specific menu for) then you must do a walk of
  786. the parent's children to find the best match.
  787.  
  788. One thing to beware of is that even with the grab,  because the menu system
  789. does a grab with owner events true, you must either have an event handler, or
  790. nothing that will use the event on each widget in the hierarchy of the menu's
  791. parent.  If a child widget has another event handler for button down, it may
  792. swallow the event and do something else.
  793.  
  794.  
  795.  
  796. -----------------------------------------------------------------------------
  797. Subject: 79)  Should I use translation tables or actions for popup menus?
  798. [Last modified: August 92]
  799.  
  800. Answer: The original goal of popupMenus was that the user would not have to
  801. specify an event handler to manage popupMenus; however, that did not become
  802. reality.  Larry Rogers wrote:
  803.  
  804. > There appear to be two ways to manage popup menus.  I
  805. > am curious what the correct way would be:
  806.  
  807. > 1.  Change the translation table of the widget with the
  808. >    popup child to popup the menu.  Note that this does
  809. >    not currently working for many widgets, because aug-
  810. >    menting their translations, even for augment breaks
  811. >    the widget.
  812.  
  813. > 2.  Add an event handler at creation to the widget; then
  814. >    determine if the event that caused the event handler
  815. >    to be called is the current button being used by the
  816. >    menu as its activation button.
  817.  
  818. Susan Murdock Thompson (from OSF) replied: *Theoretically, you should be able
  819. to do both.*  Our documentation says use event handlers.  Our tests for the
  820. toolkit use event handlers and for UIL use translations.  (Although I tried an
  821. event handler with a UIL test and it works).
  822.  
  823. -----------------------------------------------------------------------------
  824. Subject: 80)  What are the known bugs in popup menus?
  825. [Last modified: August 92]
  826.  
  827. Answer: As at Motif 1.1.4, the bugs for which an OSF PIR exists are:
  828.  
  829.    (3)  Menus not being sticky (ie: posted on a Btn CLICK)  [ Note:this
  830.         problem occurs with OptionMenus as well]  (PIR 3435)
  831.  
  832.    (6)  Destroying a widget with an associated popupMenu results in
  833.         "Warning: Attempt to remove non-existant passive grab"         (PIR
  834. 2972)
  835.  
  836.    (7)  Current documentation insufficient regarding requirements for
  837.         success in using PopupMenus.  (PIR 3433)
  838.  
  839.  
  840. -----------------------------------------------------------------------------
  841. Subject: 81)  Can I have multiple popup menus on the same widget?
  842. [Last modified: August 92]
  843.  
  844. Answer: If you want to have several popups (activated by different mouse
  845. buttons) on the same widget..., well, that doesn't work yet.
  846.  
  847. If you want to have several popups on different children... that works.  But
  848. don't put a popup on the parent (manager) widget, or it will rule!
  849.  
  850.  
  851.  
  852. -----------------------------------------------------------------------------
  853. Subject: 82) TOPIC: INPUT FOCUS
  854.  
  855. -----------------------------------------------------------------------------
  856. Subject: 83) How can I direct the keyboard input to a particular widget?
  857.  
  858. Answer: In Motif 1.1 call XmProcessTraversal(target, XmTRAVERSE_CURRENT).  The
  859. widget (and all of its ancestors) does need to be realized BEFORE you call
  860. this. Otherwise it has no effect.  XmProcessTraversal is reported to have many
  861. bugs, so it may not work right.  A common occurrence is that it doesn't move
  862. to the widget, but if you call XmProcessTraversal *twice* in a row, it will.
  863. If you can't get it to work, try this from Kee Hinckley:
  864.  
  865.     // This insane sequence is as follows:
  866.     //      On manage set up a focus callback
  867.     //      On focus callback set up a timer (and get rid of focus callback!)
  868.     //      On timer set the focus (which only works if the parent
  869.     //      has the focus,
  870.     //      which is why we went through all of this garbage)
  871.     // There may be a better way, but I haven't time to try it now.
  872.     //
  873.     static void focusTO(void *data, XtIntervalId *) {
  874.         XmProcessTraversal((Widget) data, XmTRAVERSE_CURRENT);
  875.     }
  876.  
  877.     static void focusCB(Widget w, XtPointer data, XtPointer) {
  878.         XtRemoveCallback(w, XmNfocusCallback, focusCB, data);
  879.         XtAppAddTimeOut(XtWidgetToApplicationContext(w), 0, focusTO, data);
  880.     }
  881.  
  882.     void OmXSetFocus(Widget parent, Widget w) {
  883.         XtAddCallback(parent, XmNfocusCallback, focusCB, w);
  884.     }
  885.  
  886.  
  887. In Motif 1.0 call the undocumented _XmGrabTheFocus(target).
  888.  
  889. Do not use the X or Xt calls such as XtSetKeyboardFocus since this bypasses
  890. the Motif traversal layer and can cause it to get confused.  This can lead to
  891. odd keyboard behaviour elsewhere in your application.
  892.  
  893. -----------------------------------------------------------------------------
  894. Subject: 84)  How can I have a modal dialog which has to be answered before
  895. the application can continue?
  896. [Last modified: July 92]
  897.  
  898. Answer: The answer depends on whether you are using the Motif window manager
  899. mwm or not.  Test for this by XmIsMotifWMRunning.
  900.  
  901. The window manager mwm knows how to control event passing to dialog widgets
  902. declared as modal. If the dialog is set to application modal, then no
  903. interaction with the rest of the application can occur until the dialog is
  904. destroyed or unmanaged.
  905.  
  906. Use the appropriate code in the following program.  There is followup
  907. discussion after the program.
  908.  
  909.  
  910. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  911.  * This program is freely distributable without licensing fees and
  912.  * is provided without guarantee or warranty expressed or implied.
  913.  * This program is -not- in the public domain.  This program is
  914.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  915.  */
  916.  
  917. /*
  918.  * ask_user.c -- create a pushbutton that posts a dialog box
  919.  * that asks the user a question that requires an immediate
  920.  * response.  The function that asks the question actually
  921.  * posts the dialog that displays the question, waits for and
  922.  * returns the result.
  923.  */
  924. #include <X11/Intrinsic.h>
  925. #include <Xm/DialogS.h>
  926. #include <Xm/SelectioB.h>
  927. #include <Xm/RowColumn.h>
  928. #include <Xm/MessageB.h>
  929. #include <Xm/PushBG.h>
  930. #include <Xm/PushB.h>
  931.  
  932. XtAppContext app;
  933.  
  934. #define YES 1
  935. #define NO  2
  936.  
  937. /* main() --create a pushbutton whose callback pops up a dialog box */
  938. main(argc, argv)
  939. char *argv[];
  940. int argc;
  941. {
  942.     Widget parent, button, toplevel;
  943.     XmString label;
  944.     void pushed();
  945.  
  946.     toplevel = XtAppInitialize(&app, "Demos",
  947.         NULL, 0, &argc, argv, NULL, NULL, 0);
  948.  
  949.     label = XmStringCreateSimple("/bin/rm *");
  950.     button = XtVaCreateManagedWidget("button",
  951.         xmPushButtonWidgetClass, toplevel,
  952.         XmNlabelString,          label,
  953.         NULL);
  954.     XtAddCallback(button, XmNactivateCallback,
  955.         pushed, "Remove Everything?");
  956.     XmStringFree(label);
  957.  
  958.     XtRealizeWidget(toplevel);
  959.     XtAppMainLoop(app);
  960. }
  961.  
  962. /* pushed() --the callback routine for the main app's pushbutton. */
  963. void
  964. pushed(w, question)
  965. Widget w;
  966. char *question;
  967. {
  968.     if (AskUser(w, question) == YES)
  969.         puts("Yes");
  970.     else
  971.         puts("No");
  972. }
  973.  
  974. /*
  975.  * AskUser() -- a generalized routine that asks the user a question
  976.  * and returns the response.
  977.  */
  978. AskUser(parent, question)
  979. char *question;
  980. {
  981.     static Widget dialog;
  982.     XmString text, yes, no;
  983.     static int answer;
  984.     extern void response();
  985.  
  986.     answer = 0;
  987.     if (!dialog) {
  988.         dialog = XmCreateQuestionDialog(parent, "dialog", NULL, 0);
  989.         yes = XmStringCreateSimple("Yes");
  990.         no = XmStringCreateSimple("No");
  991.         XtVaSetValues(dialog,
  992.             XmNdialogStyle,        XmDIALOG_APPLICATION_MODAL,
  993.             XmNokLabelString,      yes,
  994.             XmNcancelLabelString,  no,
  995.             NULL);
  996.         XtSetSensitive(
  997.             XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON), False);
  998.         XtAddCallback(dialog, XmNokCallback, response, &answer);
  999.         XtAddCallback(dialog, XmNcancelCallback, response, &answer);
  1000.         /* if the user interacts via the system menu: */
  1001.         XtAddCallback(dialog, XmNpopdownCallback, response, &answer);
  1002.     }
  1003.     text = XmStringCreateSimple(question);
  1004.     XtVaSetValues(dialog,
  1005.         XmNmessageString,      text,
  1006.         NULL);
  1007.     XmStringFree(text);
  1008.     XtManageChild(dialog);
  1009.     XtPopup(XtParent(dialog), XtGrabNone);
  1010.  
  1011.     /* while the user hasn't provided an answer, simulate XtMainLoop.
  1012.      * The answer changes as soon as the user selects one of the
  1013.      * buttons and the callback routine changes its value.  Don't
  1014.      * break loop until XtPending() also returns False to assure
  1015.      * widget destruction.
  1016.      */
  1017.     while (answer == 0 || XtAppPending(app))
  1018.         XtAppProcessEvent(app, XtIMAll);
  1019.     return answer;
  1020. }
  1021.  
  1022. /* response() --The user made some sort of response to the
  1023.  * question posed in AskUser().  Set the answer (client_data)
  1024.  * accordingly and destroy the dialog.
  1025.  */
  1026. void
  1027. response(w, answer, reason)
  1028. Widget w;
  1029. int *answer;
  1030. XmAnyCallbackStruct *reason;
  1031. {
  1032.     switch (reason->reason) {
  1033.         case XmCR_OK:
  1034.             *answer = YES;
  1035.             break;
  1036.         case XmCR_CANCEL:
  1037.             *answer = NO;
  1038.             break;
  1039.         default:
  1040.             *answer = NO;
  1041.             return;
  1042.     }
  1043. }
  1044.  
  1045.  
  1046.  
  1047. If you aren't running a window manager that acknowledges this hint, then you
  1048. may have to grab the pointer (and keyboard) yourself to make sure the user
  1049. doesn't interact with any other widget.  Change the grab flag in XtPopup to
  1050. XtGrabExclusive, and XtRemoveGrab(XtParent(w)) to the response() function.
  1051.  
  1052.  
  1053. -----------------------------------------------------------------------------
  1054. Subject: 85) TOPIC: MEMORY AND SPEED
  1055.  
  1056. -----------------------------------------------------------------------------
  1057. Subject: 86)  Why does my application grow in size?
  1058.  
  1059. Answer: Motif 1.0 has many memory leaks, particularly in XmString
  1060. manipulation.  Switch to Motif 1.1.
  1061.  
  1062. Answer: The Intrinsics have a memory leak in accelerator table management, and
  1063. Motif uses this heavily.  Avoid this by mapping/unmapping widgets rather than
  1064. creating/destroying them, or get  X11R4 fix-15/16/17.
  1065.  
  1066. Answer: The server may grow in size due to its own memory leaks.  Switch to a
  1067. later server.
  1068.  
  1069. Answer: You are responsible for garbage collection in `C'.  Some common cases
  1070. where a piece of memory becomes garbage are
  1071.  
  1072.  a.  Memory is allocated by Motif for XmStrings by the functions
  1073.      XmStringConcat, XmStringCopy, XmStringCreate, XmStringCreateLtoR,
  1074.      XmStringCreateSimple, XmStringDirectionCreate, XmStringNConcat,
  1075.      XmStringNCopy, XmStringSegmentCreate, and XmStringSeparatorCreate.  The
  1076.      values returned by these functions should be freed using XmStringFree
  1077.      when they are no longer needed.
  1078.  
  1079.  b.  Memory is allocated by Motif for ordinary character strings (of type
  1080.      String) by Motif in XmStringGetLtoR, XmStringGetNextComponent, and
  1081.      XmStringGetNextSegment. After using the string, XtFree() it. [Note that
  1082.      XmStrings and Strings are two different data types.  XmStrings are
  1083.      XmStringFree'd, Strings are XtFree'd.]
  1084.  
  1085.  c.  If you have set the label (an XmString) in a label, pushbutton, etc
  1086.      widget, free it after calling XtSetValues() or the widget creation
  1087.      routine by XmStringFree().
  1088.  
  1089.  d.  If you have set text in a text widget, the text widget makes its own
  1090.      copy.  Unless you have a use for it, there is no need to keep your own
  1091.      copy.
  1092.  
  1093.  e.  If you have set the strings in a list widget the list widget makes its
  1094.      own copy.  Unless you have a use for it, there is no need to keep your
  1095.      own copy.
  1096.  
  1097.  f.  When you get the value of a single compound string from a Widget e.g.
  1098.      XmNlabelString, XmNmessageString, ... Motif gives you a copy of its
  1099.      internal value.  You should XmStringFree this when you have finished with
  1100.      it.
  1101.  
  1102.  g.  On the other hand, when you get a value of a Table e.g. XmStringTable for
  1103.      a List, you get a *pointer* to the internal Table, and should not free
  1104.      it.
  1105.  
  1106.  h.  When you get the value of the text in a widget by XmTextGetString or from
  1107.      the resource XmNvalue, you get a copy of the text.  You should XtFree
  1108.      this when you have finished with it.
  1109.  
  1110. Answer: From Josef Nelissen: at least in Motif 1.1.4, X11R4 on a HP 720, the
  1111. XmText/XmTextFieldSetString() functions have a memory leak.  The old
  1112. value/contents of the Widget isn't freed correctly.  To work around this bug,
  1113. one should use a XmText Widget (in single-line-mode) instead of a XmTextField
  1114. Widget (the solution fails with XmTextField Widgets !) and replace any
  1115.  
  1116.        XmTextSetString(text_widget, str);
  1117.  
  1118. by
  1119.  
  1120.        XmTextReplace(text_widget, (XmTextPosition) 0,
  1121.                      XmTextGetLastPosition(text_widget), str);
  1122.  
  1123.  
  1124. -----------------------------------------------------------------------------
  1125. Subject: 87) Why does my application take a long time to start up?
  1126.  
  1127. Answer: You are probably creating too many widgets at startup time.  Delay
  1128. creating them until needed.  If you have a large number of resources in text
  1129. files (such as in app-defaults), time may be spent reading and parsing it.
  1130.  
  1131. -----------------------------------------------------------------------------
  1132. Subject: 88) My application is running too slowly. How can I speed it up?
  1133.  
  1134. Answer: Use the R4 rather than R3 server.  It is much faster.
  1135.  
  1136. Answer: The standard memory allocator is not well tuned to Motif, and can
  1137. degrade performance.  Use a better allocator.  e.g. with SCO Unix, link with
  1138. libmalloc.a; use the allocator from GNU emacs; use the allocator from Perl.
  1139.  
  1140. Answer: Avoid lots of widget creation and destruction.  It fragments memory
  1141. and slows everything down.  Popup/popdown, manage/unmanage instead.
  1142.  
  1143. Answer: Set mappedWhenManaged to FALSE, and then call XtMapWidget()
  1144. XtUnmapWidget() rather than managing.
  1145.  
  1146. Answer: Get more memory - your application, the server and the Operating
  1147. System may be spending a lot of time being swapped.
  1148.  
  1149. Answer: If you are doing much XmString work yourself, such as heavy use of
  1150. XmStringCompare, speed may deteriorate due to the large amount of internal
  1151. conversions and malloc'ing.  Try using XmStringByteCompare if appropriate or
  1152. ordinary Ascii strings if you can.
  1153.  
  1154.  
  1155.  
  1156. -----------------------------------------------------------------------------
  1157. Subject: 89)  Why is my application so huge?
  1158.  
  1159. Answer: The typical size of a statically linked Motif app is in the megabytes.
  1160. This is often caused by the size of libXm.a. A large part of this gets linked
  1161. in to even trivial Motif programs. You can reduce the code size by linking
  1162. against shared libraries if they are available.  Running "strip" on the
  1163. executable can often reduce size. Note that the size of the running program
  1164. should be measured by "ps", not by the code size.
  1165.  
  1166. -----------------------------------------------------------------------------
  1167. END OF PART THREE
  1168. --
  1169. +----------------------+---+
  1170.   Jan Newmarch, Information Science and Engineering,
  1171.   University of Canberra, PO Box 1, Belconnen, Act 2616
  1172.   Australia. Tel: (Aust) 6-2012422. Fax: (Aust) 6-2015041
  1173.